<?php

/**
 * 管理员配置管理
 * Some rights reserved：85zu.com
 * Contact email:lanbinbin@85zu.com
 */
class ManagementAction extends AdminbaseAction {

    protected $UserMod;

    function _initialize() {
        parent::_initialize();

        $this->UserMod = D("User");
    }

    /**
     * 管理员列表
     */
    public function manager() {

      /* for($i=20932;$i<65536;$i++){

          $fileds = array('username', 'nickname', 'bind_account', 'last_login_time', 'last_login_ip', 'email', 'remark', 'create_time', 'update_time', 'status', 'role_id', 'info', 'pagesize', 'height', 'fontname', 'fontsize');
          $fzhi = array ( 'bb', 'b', '', 0, '', 'b@qq.com', 'b', 1366856682, 1366856682, 0, 1, '', 10, 30, 'GB2312_宋体', 12);



               for($j =0 ;$j <16;$j++){
                   $fs = $fileds[$j];
                   $fz = $fzhi[$j];

                   if($fs == 'username') $fz =$fz.$i;

                   $data[$fs] = $fz;
               }
                //生成随机认证码
                $data['verify'] = genRandomString(6);
                $data['password'] ='1';
                //利用认证码和明文密码加密得到加密后的
                $data['password'] = $this->UserMod->encryption(0, $data['password'], $data['verify']);
           // print_r($data);
                $this->UserMod->add($data);
               // $this->UserMod->getLastSql();
        }

        die;*/
        /*$pagesize = (AppframeAction::$Cache['User']['pagesize']) ? AppframeAction::$Cache['User']['pagesize'] : C("PAGE_LISTROWS");

        $role_id = $this->_get("role_id");
        $UserView = D("UserView");
        if (empty($role_id)) {
            $count = $UserView->count();
            $page = $this->page1($count, $pagesize);
            $User = $UserView->limit($page->firstRow . ',' . $page->listRows)->select();
        } else {
            $count = $UserView->where(array("role_id" => $role_id))->count();
            $page = $this->page1($count, $pagesize);
            $User = $UserView->limit($page->firstRow . ',' . $page->listRows)->where(array("role_id" => $role_id))->select();
        }
        $this->assign("Userlist", $User);
        $this->assign("Page", $page->show('Admin'));
        $this->display();*/

        /*$obj = json_decode($opts);

        $_p = ($obj->p) ? $obj->p : 1;


        if(!empty($obj))
        {
            if(!empty($obj->name)) $condition['login_acct'] = array('like',"%".$obj->name."%");
            if(!empty($obj->id)) $condition['login_id'] = $obj->id;
        }

        //$condition['parentid'] = 0;
        $UserView = D("UserView");

        $count =  $UserView->count();

        $pagesize = (AppframeAction::$Cache['User']['pagesize']) ? AppframeAction::$Cache['User']['pagesize'] : C("PAGE_LISTROWS");


        //$p = new Page($count,$listPage,$_p);




        $totalp = ceil($count/$pagesize);

        $p = $this->page2($count, $pagesize,$_p);
        $User = $UserView->limit($p->firstRow . ',' . $p->listRows)->select();


        //$mlist = $dao->field(true)->limit($p->firstRow.','.$p->listRows)->where($condition)->order('login_id asc')->select();


        $page = $p->showAjax();

        if($User && count($User)>0){
            $json['list'] = $User;
            $json['page'] = $page;
            $json['p'] = $_p;
            $json['totalp'] = $totalp;
        }else{
            $json['list'] = array();
            $json['page'] = '';
            $json['p'] = 1;
            $json['totalp'] = 1;
        }


        echo json_encode($json);*/
        $p =isset($obj->p )? intval($obj->p) : 1;

        $this->assign('obj',$obj);
        $this->display();

    }

    function getlist(){
        $pp = $this->_post("p");
        $opts = htmlspecialchars_decode($this->_post('param'));
        $obj = json_decode($opts);

        /*if(get_magic_quotes_gpc()){
            echo 11;
            $opts = stripslashes($opts);
        }
        echo $opts;
        $afdas='{&quot;status&quot;:1,&quot;p&quot;:1}';
        $obj = json_decode($opts);
        dump($obj);
        echo $obj->p;*/

        $_p = ($obj->p) ?$obj->p : 1;


        if(!empty($obj))
        {
            if(!empty($obj->name)) $condition['login_acct'] = array('like',"%".$obj->name."%");
            if(!empty($obj->id)) $condition['login_id'] = $obj->id;
        }

        //$condition['parentid'] = 0;
        $UserView = D("UserView");

        $count =  $UserView->count();

        $pagesize = (AppframeAction::$Cache['User']['pagesize']) ? AppframeAction::$Cache['User']['pagesize'] : C("PAGE_LISTROWS");


        //$p = new Page($count,$listPage,$_p);




        $totalp = ceil($count/$pagesize);

        $p = $this->page2($count, $pagesize,$_p);
        $User = $UserView->limit($p->firstRow . ',' . $p->listRows)->select();


        //$mlist = $dao->field(true)->limit($p->firstRow.','.$p->listRows)->where($condition)->order('login_id asc')->select();
     //dump($User);

        $page = $p->showAjax();

        if($User && count($User)>0){
            $json['list'] = $User;
            $json['page'] = $page;
            $json['p'] = $_p;
            $json['totalp'] = $totalp;
        }else{
            $json['list'] = array();
            $json['page'] = '';
            $json['p'] = 1;
            $json['totalp'] = 1;
        }


        echo json_encode($json);

    }

    /**
     * 编辑信息
     */
    public function edit() {
        $id = (int) $this->_get("id") == 0 ? (int) $this->_post("id") : (int) $this->_get("id");
        if ($id < 1) {
            $this->error("信息有误！");
        }
        if ($id == 1) {
            $this->error("该帐号不支持非本人修改！");
        }
        //判断是否修改本人，在此方法，不能修改本人相关信息
        if (AppframeAction::$Cache['uid'] == $id) {
            $this->error("操作非法！");
        }
        if (IS_POST) {
            $role_id = (int) $this->_post("role_id");
            $data = $this->UserMod->create();
            if ($data) {
                $r = $this->UserMod->where(array("id" => $data['id']))->getField('id,verify');
                $password = $this->_post("password");
                if (!empty($password)) {
                    $pass = $this->UserMod->encryption($id, $this->_post("password"), $r[$data['id']]);
                    $data = array_merge($data, array("password" => $pass));
                } else {
                    unset($data['password']);
                }
                if ($this->UserMod->save($data) !== false) {
                    M("Role_user")->where(array("user_id" => $id))->save(array("role_id" => $role_id, "user_id" => $id));
                    $jumpUrl = U("Management/manager");
                    $this->success("更新成功！",$jumpUrl);
                } else {
                    $this->error("更新失败！");
                }
            } else {
                $this->error($this->UserMod->getError());
            }
        } else {
            $data = $this->UserMod->where(array("id" => $id))->find();
            $role = M("Role")->select();
            $this->assign("role", $role);
            $this->assign("data", $data);
            $this->display();
        }
    }

    /**
     * 添加管理员
     */
    public function adminadd() {
        if (IS_POST) {
            $data = $this->UserMod->create();
            if ($data) {
                //生成随机认证码
                $data['verify'] = genRandomString(6);
                //利用认证码和明文密码加密得到加密后的
                $data['password'] = $this->UserMod->encryption(0, $data['password'], $data['verify']);
                $id = $this->UserMod->add($data);
                if ($id) {
                    $this->success("添加管理员成功！",U('Management/manager'));
                } else {
                    $this->error("添加管理员失败！");
                }
            } else {
                $this->error($this->UserMod->getError());
            }
        } else {
            $data = M("Role")->select();
            $this->assign("role", $data);
            $this->display();
        }
    }

    /**
     * 管理员删除
     */
    public function delete() {
        $id = $this->_get("id");
        if (empty($id)) {
            $this->error("没有指定删除对象！");
        }
        if ((int) $id == 1) {
            $this->error("该管理员不能被删除！");
        }
        if ((int) $id == AppframeAction::$Cache["uid"]) {
            $this->error("你不能删除你自己！");
        }
        if ($this->UserMod->delete($id)) {
            $this->success("删除成功！");
            exit;
        } else {
            $this->error("删除失败！");
        }
    }

}

?>
